package com.smartlink.service;

import java.util.List;

import org.json.JSONObject;

import com.smartlink.dao.BuildingDaoImpl;
import com.smartlink.dao.LoginDao;
import com.smartlink.dao.SignInDao;
import com.smartlink.pojo.Building;
import com.smartlink.pojo.Classroom;
import com.smartlink.pojo.OnLineStudent;
import com.smartlink.pojo.Student;

public class SignInService {
	private SignInDao signInDao;
	private LoginDao loginDao;
	private BuildingDaoImpl buildingDaoImpl;

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public void setSignInDao(SignInDao signInDao) {
		this.signInDao = signInDao;
	}

	public void setBuildingDaoImpl(BuildingDaoImpl buildingDaoImpl) {
		this.buildingDaoImpl = buildingDaoImpl;
	}
	

	/**
	 * 判断学生是否在线
	 * 
	 * @param studentId
	 * @return
	 */
	public boolean isOnLine(String studentId) {
		OnLineStudent onLineStudent = signInDao.getOnLineStudentByStudentId(studentId);
		if (onLineStudent != null) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 判断学生是否存在 是否已经录入过人脸
	 * 
	 * @param stuNo
	 * @return
	 */
	public String isStudentExist(String stuNo) {
		JSONObject jsonObject = new JSONObject();
		Student student = loginDao.studentLogin(stuNo);
		if (student == null) {
			jsonObject.put("result", 0);
			return jsonObject.toString();
		} else {
			jsonObject.put("result", 1);
			jsonObject.put("faceFlag", student.getFaceFlag());
		}
		return jsonObject.toString();

	}

	/**
	 * 学生签到
	 * 
	 * @param number
	 * @param lat
	 * @param lon
	 */
	public int signIn(String number, int classroomId) {

		if (classroomId != -1) {
			// 签到，修改在线人数
			signInDao.addStudent(number);
			return classroomId;
		}
		return -1;

	}

	/**
	 * 获取签到统计信息
	 * 
	 * @return
	 */
	public String getSiginInfos(int classroomId) {
		JSONObject jsonObject = new JSONObject();
		int total = signInDao.getClazzTotalStudent(classroomId);
		List<Object[]> students = signInDao.getNoSiginStudent(classroomId);
		int havaSignIn = total - students.size();
		jsonObject.put("total", total);
		jsonObject.put("signInNumber", havaSignIn);
		jsonObject.put("noSiginInfo", students);
		return jsonObject.toString();
	}

	/**
	 * 修改學生脸部识别标记
	 * 
	 * @param studentId
	 * @return
	 */
	public boolean setStudentFace(String studentId) {
		boolean flag = signInDao.setStudentFace(studentId);
		return flag;

	}

	// **********************************************************

	/**
	 * 获取教室id
	 * 
	 * @param buildingId
	 * @param lat
	 * @param lon
	 * @return
	 */
	@Deprecated
	public int signInClassroom(int buildingId, double lat, double lon) {
		List<Classroom> classrooms = signInDao.getClassroomByBuildingId(buildingId);
		double lat1;
		double lat2;
		double lon1;
		double lon2;
		for (Classroom classroom : classrooms) {
			lat1 = classroom.getLat1();
			lat2 = classroom.getLat2();
			lon1 = classroom.getLon1();
			lon2 = classroom.getLon2();
			if (isBetween(lat, lat1, lat2) && isBetween(lon, lon1, lon2)) {
				return classroom.getId();
			}

		}
		return -1;
	}

	/**
	 * 获取签到位所在的楼
	 * 
	 * @param lat
	 * @param lon
	 * @return
	 */
	@Deprecated
	public int signInBuilding(double lat, double lon) {
		List<Building> buildings = buildingDaoImpl.getAll();
		double lat1;
		double lat2;
		double lon1;
		double lon2;
		for (Building building : buildings) {
			lat1 = building.getLat1();
			lat2 = building.getLat2();
			lon1 = building.getLon1();
			lon2 = building.getLon2();
			if (isBetween(lat, lat1, lat2) && isBetween(lon, lon1, lon2)) {
				return building.getId();
			}
		}
		return -1;

	}

	/**
	 * 根据经纬度获取教室id
	 * 
	 * @param number
	 * @param lat
	 * @param lon
	 */
	@Deprecated
	public int getClassroomIdByLatLon(String number, double lat, double lon) {

		int buildingId = signInBuilding(lat, lon);
		if (buildingId != -1) {
			// 获取教室id
			int classroomId = signInClassroom(buildingId, lat, lon);
			if (classroomId != -1) {
				return classroomId;
			}
			return -1;
		}
		return -1;

	}

	/**
	 * 判断value是否在俩个数之间
	 * 
	 * @param value
	 * @param first
	 * @param second
	 * @return
	 */
	@Deprecated
	public boolean isBetween(double value, double first, double second) {
		double max = first > second ? first : second;
		double min = first < second ? first : second;
		if (value >= min && value <= max) {
			return true;
		}
		return false;

	}

}
